package com.javastudy.autowiredStudy.springCacheStudy;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 *
 * @author mac
 * @date 2021/9/8 1:47 下午
 */
@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager(){
        /**
         * 为什么这么写请看spring cache的架构
         */
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        Caffeine<Object, Object> cache = Caffeine.newBuilder()
                //最后一次写入之后开始算起 指定时间内过期
                .expireAfterWrite(10, TimeUnit.SECONDS)
                //缓存的最大条数
                .maximumSize(1000);
        //将缓存添加到缓存管理组件中
        caffeineCacheManager.setCaffeine(cache);
        return caffeineCacheManager;
    }
}
